﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_Management.Domain;
using Traceability_Management.ErrorCode;
using Traceability_Management.Infrastructure.Interfaces;
using Traceability_Management.Read.Api.Application.Command.RabitsWay;

namespace Traceability_Management.Read.Api.Application.CommandHandler.RabitsWay
{
    public class UpdateCheckImagesCommandHandler : IRequestHandler<UpdateCheckImagesCommand, ApiResult<int>>
    {
        IBaseReponsitory<Rabits> Rabits;

        public UpdateCheckImagesCommandHandler(IBaseReponsitory<Rabits> Rabits)
        {
            this.Rabits = Rabits;
        }

        public async Task<ApiResult<int>> Handle(UpdateCheckImagesCommand request, CancellationToken cancellationToken)
        {
            var Uplist = await Rabits.GetQuery().Where(c => c.Rabit_Batch == request.Batch).FirstOrDefaultAsync();
            if (Uplist != null)
            {
                if (request.CheckImages != null)
                {
                    Uplist.Rabit_CheckImg = request.CheckImages;
                }
                await Rabits.UpdateAsync(Uplist);
                return new ApiResult<int>
                {
                    Code = ApiEnum.Success,
                    Message = "保存成功",
                    Data = 1
                };
            }
            else 
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "不能修改空图",
                    Data = -1
                };
            }
        }
    }
}
